import { request } from '../utils'
import { Toast } from 'vant'

/*
  获取用户地址
*/
const getUserAddress = (requestData, cb) => {
  request({
    url: "api/shippingaddress/GetShippingAddressList",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取用户收货地址错误...")
      Toast(res.data.Msg)
    }
  })
}

/*
  新增用户地址
*/
const newAddress = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/shippingaddress/PostAddShippingAddress",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log('新增收货地址错误...')
      Toast( res.data.Msg )
    }
  })
}

/*
  编辑用户地址
*/
const editAddress = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/shippingaddress/PostEditShippingAddress",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("编辑收货地址错误...")
      Toast(res.data.Msg)
    }
  })
}

/*
  删除用户地址
*/
const deleteAddress = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/shippingaddress/PostDeleteShippingAddress",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("删除收货地址错误...")
      Toast(res.data.Msg)
    }
  })
}

/*
  设置默认地址
*/
const defaultAddress = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/shippingaddress/PostSetDefaultAddress",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("设置默认收货地址错误...")
      Toast(res.data.Msg)
    }
  })
}

export { 
  getUserAddress, 
  newAddress, 
  editAddress, 
  deleteAddress, 
  defaultAddress 
}